package com.sky.service.UserService.impl;

import com.alibaba.fastjson.JSON;
import com.sky.WebService.WebSocketServer;
import com.sky.mapper.user.UserOrderMapper;
import com.sky.pojo.Orders;
import com.sky.service.UserService.PaySuccessService;
import com.sky.vo.OrderAdvicelnfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
@Slf4j
public class PaySuccessServiceImpl implements PaySuccessService {
    @Resource
    UserOrderMapper orderMapper;
    @Resource
    WebSocketServer webSocketServer;

    @Override
    public int success(String outTradeNo) {
        Orders orders = Orders.builder().number(outTradeNo)
                .status(Orders.TO_BE_CONFIRMED)
                .checkoutTime(LocalDateTime.now())
                .payStatus(Orders.PAID)
                .build();
        int i = orderMapper.updateOutTradeNo(orders);
        log.warn("订单下单后修改的状态:{}",i);
        // 用户支付成功后 进行通知有新的订单
        OrderAdvicelnfo orderAdvicelnfo = OrderAdvicelnfo.builder().type(1).orderId(outTradeNo).content("您有新的订单,请及时处理").build();
        String result = JSON.toJSONString(orderAdvicelnfo);
        webSocketServer.sendToAllClient(result);
        log.warn("执行到webSocketServer----------");
        return i;
    }
}
